package com.surelution.projects.congress

class ReportController {

    def index = {
		def meetingId = params.meetingId
		def currentMeeting
		if(meetingId) {
			currentMeeting = Meeting.get(meetingId)
		}
		def now = new Date()
		def cri = Meeting.createCriteria()
		
		if(!currentMeeting) {
			def meetings = cri.list {
				le("checkInTimeStart", now)
				ge("meetingEnd", now)
			}
			
			if(meetings) {
				currentMeeting = meetings.get(0)
			}
		}
		if(currentMeeting) {
			def attendeeCount = 0
			def attendees
			if(currentMeeting.standing) {
				attendees = Member.findAllByLevel(MemberLevel.STANDING)
			} else {
				attendees = Member.list()
			}
			def checkInCri = CheckIn.createCriteria()
			def checkIns = checkInCri.list {
				ge("checkInTime", currentMeeting.checkInTimeStart)
				le("checkInTime", currentMeeting.meetingEnd)
			}
			if(attendees) {
				attendeeCount = attendees.size()
				checkIns.each {checkIn->
					attendees.remove(checkIn.member)
				}
			}
			return ["currentMeeting":currentMeeting, "attendeeCount":attendeeCount, 
				"atMeetingCount":attendeeCount - attendees.size(), "absents":attendees]
		}
		redirect(action:selectMeeting)
	}
	
	def selectMeeting = {}
	
	def meeting = {
		def meeting
		if(!params.meeting) {
			def now = new Date()
			meeting = Meeting.findByCheckInTimeStartLessThanAndCheckInTimeEndGreaterThan(now, now)
		} else {
			meeting = Meeting.get(params.meeting.id)
		}
		
		if(!meeting) {
			redirect(action:chooseMeeting)
		}
		
		
	}
}
